//
//  LGTMainLoginVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/11.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTMainLoginVC: LGTBaseVC, UITextViewDelegate {

    var agrreenTextView:UITextView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.setUI()
        self.addNote()
    }
    
    func addNote(){
        //微信登录回掉
        NotificationCenter.default.addObserver(self, selector: #selector(wxAutheAction), name: WX_Authe_Note, object: nil)
        //登录成功
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name: Login_Success_Note, object: nil)
        //WX_Authe_Note
    }
    
    @objc func wxAutheAction(note:NSNotification){
        LGTViewUtil.share.loginAction(nav: self.navigationController, json: note.userInfo as? Dictionary<String, Any>)
    }
    
    @objc func loginSuccess(note:NSNotification){
        let vcs = self.navigationController?.viewControllers ?? []
        var i = 0
        for vc in vcs{
            if vc == self{
                let popVC = vcs[i - 1]
                self.navigationController?.popToViewController(popVC, animated: true)
                break
            }
            i += 1
        }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: WX_Authe_Note, object: nil)
        NotificationCenter.default.removeObserver(self, name: Login_Success_Note, object: nil)
    }
    
    override func setUI() {
        super.setUI()
        
        self.setBavBarStyle(navBarHiden: false, title: "", back: true)
        //logo
        let logoImageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "zhu_icon")
        self.view.addSubview(logoImageView)
        logoImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.width.equalTo(73 * KRate)
            make.height.equalTo(63 * KRate)
            make.top.equalTo(163 * KRate + IPHONENAV_DIS)
        }
        
        let appNameLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: BlackColor, fontSize: CGFloat(24 * KRate))
        self.view.addSubview(appNameLabel)
        appNameLabel.snp.makeConstraints { (make) in
            make.height.equalTo(24 * KRate)
            make.centerX.equalTo(self.view)
            make.top.equalTo(logoImageView.snp_bottomMargin).offset(18 * KRate)
        }
        
        //微信登陆
        let wxLoginBtn:UIButton = UIButton.init()
        wxLoginBtn.backgroundColor = BaseUtil.baseUtil.getHexColor(colorHex: 0x00CE12)
        wxLoginBtn.setTitle("微信一键登录", for: UIControl.State.normal)
        wxLoginBtn.setTitle("微信一键登录", for: UIControl.State.selected)
        wxLoginBtn.setImage(UIImage.init(named: "wechatlogin"), for: UIControl.State.normal)
        wxLoginBtn.setImage(UIImage.init(named: "wechatlogin"), for: UIControl.State.selected)
        wxLoginBtn.imageEdgeInsets = UIEdgeInsets.init(top: SizeFitFloat(3.0), left: SizeFitFloat(0), bottom: SizeFitFloat(3.0), right: SizeFitFloat(11.0))
        wxLoginBtn.imageView?.contentMode = UIView.ContentMode.scaleAspectFill
        wxLoginBtn.titleLabel?.font = Font_Normal_17
        wxLoginBtn.isHighlighted = false
        wxLoginBtn.addTarget(self, action: #selector(wxLoginAction), for: UIControl.Event.touchUpInside)
        
        wxLoginBtn.layer.masksToBounds = true
        wxLoginBtn.layer.cornerRadius = CGFloat(KRate * 20)
        self.view.addSubview(wxLoginBtn)
        wxLoginBtn.snp.makeConstraints { (make) in
            make.height.equalTo(40 * KRate)
            make.left.equalTo(70 * KRate)
            make.right.equalTo(-70 * KRate)
            make.top.equalTo(appNameLabel.snp_bottomMargin).offset(KRate * 159)
        }
        
        self.agrreenTextView = LGTViewCreateUtil.share.createAgreenText()
        self.agrreenTextView!.delegate = self
        self.view.addSubview(self.agrreenTextView!)
        self.agrreenTextView?.snp.makeConstraints({ (make) in
            //make.left.equalTo(wxLoginBtn)
            make.centerX.equalTo(self.view)
            make.height.equalTo(KRate * 30)
            make.width.equalTo(KRate * 240)
            make.top.equalTo(wxLoginBtn.snp_bottomMargin).offset(KRate * 10)
        })
        
        //其他方式
        let otherLoginLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: GrayColor, fontSize: CGFloat(14 * KRate), text: "其他登录方式")
        self.view.addSubview(otherLoginLabel)
        otherLoginLabel.snp.makeConstraints { (make) in
            make.height.equalTo(14 * KRate)
            make.centerX.equalTo(self.view)
            make.top.equalTo(wxLoginBtn.snp_bottomMargin).offset(KRate * 111)
        }
        
        let phoneBtn = LGTViewCreateUtil.share.createButton(image: "mobilelogin", selected: "mobilelogin")
        self.view.addSubview(phoneBtn)
        phoneBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self.view.snp_centerXWithinMargins).offset(-26 * KRate)
            make.width.height.equalTo(44 * KRate)
            make.top.equalTo(otherLoginLabel.snp_bottomMargin).offset(KRate * 31)
        }
        
        let paawordBtn = LGTViewCreateUtil.share.createButton(image: "passwordlogin", selected: "passwordlogin")
        self.view.addSubview(paawordBtn)
        paawordBtn.snp.makeConstraints { (make) in
            make.left.equalTo(self.view.snp_centerXWithinMargins).offset(26 * KRate)
            make.width.height.equalTo(44 * KRate)
            make.top.equalTo(otherLoginLabel.snp_bottomMargin).offset(KRate * 31)
        }
        
        phoneBtn.tag = 0;
        phoneBtn.addTarget(self, action: #selector(loginAction)
            , for: UIControl.Event.touchUpInside)

        paawordBtn.tag = 1;
        paawordBtn.addTarget(self, action: #selector(loginAction)
            , for: UIControl.Event.touchUpInside)
    }
    
    @objc func loginAction(btn:UIButton){
        let loginVC = LGTPhoneLoginVC()
        loginVC.loginType = btn.tag
        self.navigationController?.pushViewController(loginVC, animated: true)
    }
    
    //点击跳转
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        let url = "\(AppModel.share?.m_host ?? "")/\(Agreen_Html_URl)"
        let webVC = LGTBaseWebVC()
        webVC.url = url
        self.navigationController?.pushViewController(webVC, animated: true)
        return false
    }
    
    @objc func wxLoginAction(){
        let req = SendAuthReq.init()
        req.scope = "snsapi_userinfo"
        req.state = "123"
        WXApi.send(req)
        
        self.view.isUserInteractionEnabled = false
        weak var weakSelf = self
        let deadline = DispatchTime.now() + 1.0
        DispatchQueue.global().asyncAfter(deadline: deadline, execute: DispatchWorkItem.init(block: {
            weakSelf?.view.isUserInteractionEnabled = true
        }))
        
    }
}
